package com.cskaoyan.service.back.admin;

import com.cskaoyan.bean.bo.back.admin.AdminBo;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.back.admin.BaseDataVo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.util.ParamUtils;
import com.cskaoyan.util.StringToLikeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MarketAddressMapper addressMapper;
    @Autowired
    private MarketCollectMapper collectMapper;
    @Autowired
    private MarketFootprintMapper footprintMapper;
    @Autowired
    private MarketSearchHistoryMapper searchHistoryMapper;
    @Autowired
    private MarketFeedbackMapper feedbackMapper;


    /**
     * 得到用户通过id
     *
     * @param id id
     * @return {@link MarketUser}
     */
    @Override
    public MarketUser getUserById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    /**
     * 通过对象参数获取用户列表
     *
     * @param parameter 参数
     * @return {@link BaseDataVo}<{@link MarketUser}>
     */
    @Override
    public BaseDataVo<MarketUser> getUserListByParam(AdminBo parameter) {
        PageHelper.startPage(parameter.getPage(), parameter.getLimit());

        MarketUserExample userExample = new MarketUserExample();

        userExample.setOrderByClause(parameter.getSort() + " " + parameter.getOrder());

        MarketUserExample.Criteria criteria = userExample.createCriteria().andDeletedEqualTo(false);

        if (ParamUtils.isValid(parameter.getUsername())) {
            criteria.andUsernameLike(StringToLikeUtil.addPercentSign(parameter.getUsername()));
        }
        if (ParamUtils.isValid(parameter.getMobile())) {
            criteria.andMobileLike(StringToLikeUtil.addPercentSign(parameter.getMobile()));
        }

        List<MarketUser> users = userMapper.selectByExample(userExample);

        PageInfo<MarketUser> userPageInfo = new PageInfo<>(users);

        return new BaseDataVo<>(userPageInfo, users);
    }

    /**
     * 获得地址列表
     *
     * @param parameter 参数
     * @return {@link BaseDataVo}<{@link MarketAddress}>
     */
    @Override
    public BaseDataVo<MarketAddress> getAddressList(AdminBo parameter) {
        PageHelper.startPage(parameter.getPage(), parameter.getLimit());

        MarketAddressExample addressExample = new MarketAddressExample();

        addressExample.setOrderByClause(parameter.getSort() + " " + parameter.getOrder());

        MarketAddressExample.Criteria criteria = addressExample.createCriteria().andDeletedEqualTo(false);

        if (ParamUtils.isValid(parameter.getUserId())) {
            criteria.andUserIdEqualTo(parameter.getUserId());
        }
        if (ParamUtils.isValid(parameter.getName())) {
            criteria.andNameLike(StringToLikeUtil.addPercentSign(parameter.getName()));
        }

        List<MarketAddress> addresses = addressMapper.selectByExample(addressExample);

        PageInfo<MarketAddress> addressPageInfo = new PageInfo<>(addresses);

        return new BaseDataVo<>(addressPageInfo, addresses);
    }

    /**
     * 得到收集名单
     *
     * @param parameter 参数
     * @return {@link BaseDataVo}<{@link MarketCollect}>
     */
    @Override
    public BaseDataVo<MarketCollect> getCollectList(AdminBo parameter) {
        PageHelper.startPage(parameter.getPage(), parameter.getLimit());

        MarketCollectExample collectExample = new MarketCollectExample();

        collectExample.setOrderByClause(parameter.getSort() + " " + parameter.getOrder());

        MarketCollectExample.Criteria criteria = collectExample.createCriteria().andDeletedEqualTo(false);

        if (ParamUtils.isValid(parameter.getUserId())) {
            criteria.andUserIdEqualTo(parameter.getUserId());
        }
        if (ParamUtils.isValid(parameter.getValueId())) {
            criteria.andValueIdEqualTo(parameter.getValueId());
        }

        List<MarketCollect> collects = collectMapper.selectByExample(collectExample);

        PageInfo<MarketCollect> collectPageInfo = new PageInfo<>(collects);

        return new BaseDataVo<>(collectPageInfo, collects);
    }

    /**
     * 得到足迹列表
     *
     * @param parameter 参数
     * @return {@link BaseDataVo}<{@link MarketFootprint}>
     */
    @Override
    public BaseDataVo<MarketFootprint> getFootprintList(AdminBo parameter) {
        PageHelper.startPage(parameter.getPage(), parameter.getLimit());

        MarketFootprintExample footprintExample = new MarketFootprintExample();

        footprintExample.setOrderByClause(parameter.getSort() + " " + parameter.getOrder());

        MarketFootprintExample.Criteria criteria = footprintExample.createCriteria().andDeletedEqualTo(false);

        if (ParamUtils.isValid(parameter.getUserId())) {
            criteria.andUserIdEqualTo(parameter.getUserId());
        }
        if (ParamUtils.isValid(parameter.getGoodsId())) {
            criteria.andGoodsIdEqualTo(parameter.getGoodsId());
        }

        List<MarketFootprint> footprints = footprintMapper.selectByExample(footprintExample);

        PageInfo<MarketFootprint> footprintPageInfo = new PageInfo<>(footprints);

        return new BaseDataVo<>(footprintPageInfo, footprints);
    }

    /**
     * 让历史列表
     *
     * @param parameter 参数
     * @return {@link BaseDataVo}<{@link MarketSearchHistory}>
     */
    @Override
    public BaseDataVo<MarketSearchHistory> getHistoryList(AdminBo parameter) {
        PageHelper.startPage(parameter.getPage(), parameter.getLimit());
        //new example
        MarketSearchHistoryExample searchHistoryExample = new MarketSearchHistoryExample();
        searchHistoryExample.clear();
        //添加排序条件
        searchHistoryExample.setOrderByClause(parameter.getSort() + " " + parameter.getOrder());
        //建立查找条件
        MarketSearchHistoryExample.Criteria criteria = searchHistoryExample.createCriteria().andDeletedEqualTo(false);
        //添加查找条件
        //判断参数是合法
        if (ParamUtils.isValid(parameter.getUserId())) {
            criteria.andUserIdEqualTo(parameter.getUserId());
        }
        if (ParamUtils.isValid(parameter.getKeyword())) {
            criteria.andKeywordLike(StringToLikeUtil.addPercentSign(parameter.getKeyword()));
        }
        //查找并返回
        List<MarketSearchHistory> searchHistories = searchHistoryMapper.selectByExample(searchHistoryExample);
        //分页
        PageInfo<MarketSearchHistory> searchHistoryPageInfo = new PageInfo<>(searchHistories);
        //返回json数据
        return new BaseDataVo<>(searchHistoryPageInfo, searchHistories);
    }

    /**
     * 得到反馈列表
     *
     * @param parameter 参数
     * @return {@link BaseDataVo}<{@link MarketFeedback}>
     */
    @Override
    public BaseDataVo<MarketFeedback> getFeedbackList(AdminBo parameter) {

        PageHelper.startPage(parameter.getPage(), parameter.getLimit());

        MarketFeedbackExample feedbackExample = new MarketFeedbackExample();

        feedbackExample.setOrderByClause(parameter.getSort() + " " + parameter.getOrder());

        MarketFeedbackExample.Criteria criteria = feedbackExample.createCriteria().andDeletedEqualTo(false);

        if (ParamUtils.isValid(parameter.getUsername())) {
            criteria.andUsernameEqualTo(parameter.getUsername());
        }
        if (ParamUtils.isValid(parameter.getId())) {
            criteria.andIdEqualTo(parameter.getId());
        }

        List<MarketFeedback> feedbacks = feedbackMapper.selectByExample(feedbackExample);

        PageInfo<MarketFeedback> feedbackPageInfo = new PageInfo<>(feedbacks);

        return new BaseDataVo<>(feedbackPageInfo, feedbacks);
    }


    /**
     * 更新用户通过json参数
     *
     * @param user 用户
     */
    @Override
    public void updateUserByJsonParam(MarketUser user) {

        userMapper.updateByPrimaryKey(user);
    }
}
